import {Component, OnInit} from '@angular/core';
import {RechargeService} from '../recharge.service';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-promotion',
  templateUrl: './promotion.component.html',
  styleUrls: ['./promotion.component.scss']
})
export class PromotionComponent implements OnInit {

  // 查询条件
  query = {
    name: '',
    type: '',
    status: '',
    // 当前页
    page: 1,
    // 分页大小
    pageSize: 20,
  };
  // 表格数据
  listOfData = {
    page: 1,
    total: 100,
    records: []
  };
  trackModal = {
    visible: false,
    query: {
      activityId: null,
      orderNum: null,
      userId: null,
      status: [],
      time: [],
      payTime: [],
      page: 1,
      pageSize: 10
    },
    listOfData: {
      page: 1,
      total: 100,
      records: []
    }
  };
  orderStatus = [
    '待付款',
    '充值中',
    '交易完成',
    '支付超时',
    '客户取消',
    '客服取消',
    '交易结束',
    '充值失败',
  ];

  constructor(
    public rechargeService: RechargeService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.getTableData();
  }

  onPageIndexChange(index: number) {
    this.query.page = index;
    this.getTableData();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    this.getTableData();
  }

  onTrackPageIndexChange(index: number) {
    this.trackModal.query.page = index;
    this.getTrackList();
  }

  onTrackPageSizeChange(pageSize: number) {
    this.trackModal.query.pageSize = pageSize;
    this.getTrackList();
  }

  queryBtn() {
    this.query.page = 1;
    this.getTableData();
  }

  resetCondition() {
    this.query = {
      name: '',
      type: '',
      status: '',
      page: 1,
      pageSize: 20,
    };
    this.queryBtn();
  }

  getTableData() {
    this.listOfData.records = [];
    this.rechargeService.getPromotionList(this.query).then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data;
      }
    });
  }

  deleteRow(id: any) {
    this.rechargeService.deletePromotion(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.getTableData();
      }
    });
  }

  hideTrackModal() {
    this.trackModal = {
      visible: false,
      query: {
        activityId: null,
        orderNum: null,
        userId: null,
        status: [],
        time: [],
        payTime: [],
        page: 1,
        pageSize: 10
      },
      listOfData: {
        page: 1,
        total: 100,
        records: []
      }
    };
  }

  changeStatus(id: any, status: number) {
    this.rechargeService.editPromotion({id, status}).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getTableData();
      }
    });
  }

  async showTrack(id: any) {
    this.trackModal.query.activityId = id;
    await this.getTrackList();
    this.trackModal.visible = true;
  }

  resetTrackCondition() {
    this.trackModal.query = {
      activityId: this.trackModal.query.activityId,
      orderNum: null,
      userId: null,
      status: [],
      time: [],
      payTime: [],
      page: 1,
      pageSize: 10
    };
    this.getTrackList();
  }

  queryTrackBtn() {
    this.trackModal.query.page = 1;
    this.getTrackList();
  }

  getTrackList() {
    this.trackModal.listOfData.records = [];
    return this.rechargeService.getPromotionTrackList({
      activityId: this.trackModal.query.activityId,
      orderNum: this.trackModal.query.orderNum,
      userId: this.trackModal.query.userId,
      statusStr: this.trackModal.query.status.join(','),
      page: this.trackModal.query.page,
      pageSize: this.trackModal.query.pageSize,
      orderBeginTime: this.rechargeService.formatTime(this.trackModal.query.time[0], 'date'),
      orderEndTime: this.rechargeService.formatTime(this.trackModal.query.time[1], 'date'),
      createPayTime: this.rechargeService.formatTime(this.trackModal.query.payTime[0], 'date'),
      endPayTime: this.rechargeService.formatTime(this.trackModal.query.payTime[1], 'date'),
    }).then((res: any) => {
      console.log(res);
      if (res.code === 0) {
        this.trackModal.listOfData = res.data;
      }
    });
  }
}
